package com.negative.activities;

import java.text.SimpleDateFormat;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;

import java.text.DateFormat;
import java.util.Locale;

import com.negative.data.EventItem;
import com.negative.logic.MenuListener;
import com.negative.utils.Utils;

import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class EventActivity extends Activity {

	EventItem event;
	ImageView eventPic;
	TextView date, description, title;
	ImageView menuButton;
	Button purchaseButton;
	MenuListener menuListener;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_eventpage);
		
		event = (EventItem) this.getIntent().getSerializableExtra("EventItem");
		
		initialize();
		
		title.setText(event.getName());
		
		if (event.getPicture() != null && !event.getPicture().isEmpty()) {
		final Bitmap icon = ((NegativeApplication)getApplication()).getEventPic(event.getPicture());
		runOnUiThread(new Runnable() {
			public void run() {
				eventPic.setImageBitmap(icon);
				}
			});	
		}
		
		date = (TextView) findViewById(R.id.event_page_date_label);
		DateFormat dateFormat = new SimpleDateFormat("EEEE dd/MM/yyyy HH:mm", new Locale("iw-il"));
		date.setText(getResources().getString(R.string.DateLabel) + dateFormat.format(event.getSubEvents().get(0).getStart_time()));
		
		description = (TextView) findViewById(R.id.event_page_description_label);
		String sp = Html.fromHtml(event.getDescription().trim()).toString();
		description.setText(Utils.cleanHtml(sp));
		purchaseButton = (Button)findViewById(R.id.event_page_purch_button);
		purchaseButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = null;
//				String adUrl = getRandomAdUrl();
//				if (adUrl != null) {
//					intent = new Intent(EventActivity.this, AdActivity.class);
//					intent.putExtra("URL", adUrl);
//					intent.putExtra("Event", event);
//				} else {
					intent = new Intent(EventActivity.this, TicketTypeActivity.class);
					intent.putExtra("Event", event);
//				}
				startActivity(intent);
			}
		});
	}

	
	private void initialize() {
		menuListener = new MenuListener(this);
		title = (TextView) findViewById(R.id.event_page_title_label);
		eventPic = (ImageView) findViewById(R.id.event_page_picture);
		menuButton = (ImageView) findViewById(R.id.menu_icon);
		
		menuButton.setOnClickListener(menuListener);
	}
	
//	private String getRandomAdUrl() {
//		String url = null;
//		NegativeApplication app = (NegativeApplication) getApplication();
//		if (app.getAdUrls().size() != 0) {
//			int index = (int) (Math.random() * (app.getAdUrls().size() - 1));
//			url = app.getAdUrls().get(index);
//		}
//		return url;
//	}
}
